import { lazy, ReactNode, Suspense, useState, useEffect } from "react"
import Redirect from './Redirect';
import { AuthRouter } from './AuthRouter'
import AppLayout from '@/layout/index.jsx'
import { useRoutes, Navigate } from "react-router-dom"
import {
  filterRouterAsync, filterRouter
} from '@/store/features/routeSlice'
import { useSelector, useDispatch } from 'react-redux'
import Loading from "./Loading"
const modules = import.meta.glob('@/pages/**')
//解决白屏现象   快速导入工具函数
const lazyLoad = (children) => {
  if (modules.hasOwnProperty(`/src/pages${children}/index.jsx`)) {
    const Module = lazy(modules[`/src/pages${children}/index.jsx`])
    return <Suspense fallback={<Loading />}> <Module /></Suspense>
  } else {
    return <Redirect to="/404" />
  }
}
const defaultRoutes = [
  {
    path: '/',
    element: <AppLayout></AppLayout>,
    children: [
      {
        path: '',
        element: <AuthRouter> {lazyLoad('/home')}</AuthRouter>
      },
      {
        path: 'home',
        element: <AuthRouter> {lazyLoad('/home')}</AuthRouter>
      },
      // {
      //   path: 'demo',
      //   element: <AuthRouter> {lazyLoad('/demo/list')}</AuthRouter>
      // },
      // {
      //   path: 'demo/add',
      //   element: <AuthRouter> {lazyLoad('/demo/list/components/add')}</AuthRouter>
      // },
      // 商品管理/查看详情
      {
        path: 'goodsList/detailsGoods',
        element: <AuthRouter> {lazyLoad('/goodsManagement/goodsList/components/detailsGoods')}</AuthRouter>
      },
      //商品管理/编辑商品
      {
        path: 'goodsList/editGoods',
        element: <AuthRouter> {lazyLoad('/goodsManagement/goodsList/components/addGoods')}</AuthRouter>
      },
      //商品管理/新建商品
      {
        path: 'goodsList/addGoods',
        element: <AuthRouter> {lazyLoad('/goodsManagement/goodsList/components/addGoods')}</AuthRouter>
      },
      {
        path: 'billList/goodsDetails',
        element: <AuthRouter> {lazyLoad('/fundsManage/billList/components/goodsDetails')}</AuthRouter>
      },
      {
        // 秒杀活动添加|编辑|查看
        path: 'marketingTool/kill/event',
        element: <AuthRouter> {lazyLoad('/marketingTool/kill/components/event')}</AuthRouter>
      },
      {
        // 优惠券定时任务|新建|查看
        path: 'couponManagement/timedTask/task',
        element: <AuthRouter> {lazyLoad('/couponManagement/timedTask/components/add')}</AuthRouter>
      },
      {
        // 优惠券定时任务结果
        path: 'couponManagement/timedTask/result',
        element: <AuthRouter> {lazyLoad('/couponManagement/timedTask/components/result')}</AuthRouter>
      },
      {
        // 优惠券列表|添加|编辑
        path: 'couponManagement/couponList/coupon',
        element: <AuthRouter> {lazyLoad('/couponManagement/couponsList/components/add')}</AuthRouter>
      },
      {
        // 商城订单详情
        path: "/orderManage/mallOrder/detail",
        element: <AuthRouter> {lazyLoad('/orderManage/mallOrder/detail')}</AuthRouter>
      },
      {
        // 售后订单详情
        path: 'orderManage/afterSaleOrder/detail',
        element: <AuthRouter> {lazyLoad('/orderManage/afterSaleOrder/detail')}</AuthRouter>
      },
      {
        // 拼团活动添加|编辑|查看
        path: 'spellTogether/list/event',
        element: <AuthRouter> {lazyLoad('/spellTogether/list/components/event')}</AuthRouter>
      },
      {
        // 拼团活动数据看板
        path: 'spellTogether/list/result',
        element: <AuthRouter> {lazyLoad('/spellTogether/list/components/result')}</AuthRouter>
      },
      {
        // 活动管理 新建|编辑
        path: 'activityManager/list/event',
        element: <AuthRouter> {lazyLoad('/activityManager/list/components/event')}</AuthRouter>
      },
      {
        // 活动管理 参与用户
        path: 'activityManager/list/user',
        element: <AuthRouter> {lazyLoad('/activityManager/list/components/joinUser')}</AuthRouter>
      },
      {
        // 优惠券列表|领取记录
        path: 'couponManagement/couponList/receipt',
        element: <AuthRouter> {lazyLoad('/couponManagement/couponsList/components/receipt')}</AuthRouter>
      },
      {
        // 优惠券列表|CDK列表
        path: 'couponManagement/couponList/CDKlist',
        element: <AuthRouter> {lazyLoad('/couponManagement/couponsList/components/CDKlist')}</AuthRouter>
      },{
        // 新增运费模板
        path: 'goodsManagement/freightTemplate/create',
        element: <AuthRouter> {lazyLoad('/goodsManagement/freightTemplate/create')}</AuthRouter>
      },
    ]
  },
  {
    path: '/login',
    element: <>{lazyLoad('/login')} </>
  },
  {
    path: '/404',
    title: "notFound",
    element: lazyLoad('/404'),
  },
  {
    path: "*",
    title: "notFound",
    element: <AuthRouter>{lazyLoad('/404')}</AuthRouter>,
  },
]
export default function IndexRouter() {
  let { routes } = useSelector(state => state.route)
  const [router, setRouter] = useState([])
  useEffect(() => {
    const result = []
    routes.forEach(item => {
      if (item.menuLevel == 2) {
        result.push({
          path: item.menuUrl,
          element: <AuthRouter>{lazyLoad(item.menuUrl)}</AuthRouter>
        })
      }
    })
    defaultRoutes[0].children.push(...result)
    setRouter(defaultRoutes)
  }, [routes])
  const element = useRoutes(router)
  return element
}


